package org.pine.controller;

import lombok.extern.slf4j.Slf4j;
import org.pine.model.Book;
import org.pine.service.BookService;
import org.pine.service.BookService2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName BookController
 * @Description:TODO
 * @Author liucs
 * @Date 2021-01-28
 * @Version V1.0
 */
@RestController
@Slf4j
public class BookController {

//    BookService bookService;

    BookService2 bookService2;
    @Autowired
    public void setBookService2(BookService2 bookService2) {
        this.bookService2 = bookService2;
    }

   /* @Autowired
    public void setBookService(BookService bookService) {
        this.bookService = bookService;
    }*/

    @GetMapping("/findAll")
    public void findAll() {
        PageRequest pageable = PageRequest.of(2, 3);
        Page<Book> page = bookService2.getBookByPage(pageable);
        System.out.println("总页数:"+page.getTotalPages());
        System.out.println("总记录数:"+page.getTotalElements());
        System.out.println("查询结果:"+page.getContent());
        System.out.println("当前页数:"+(page.getNumber()+1));
        System.out.println("当前页记录数:"+page.getNumberOfElements());
        System.out.println("每页记录数:"+page.getSize());
    }
    @GetMapping("/search")
    public void search() {
        List<Book> bs1 = bookService2.getBookByIdAndAuthor("鲁迅", 7);
        List<Book> bs2 = bookService2.getBooksByAuthorStartingWith("吴");
        List<Book> bs3 = bookService2.getBooksByIdAndName("西", 8);
        List<Book> bs4 = bookService2.getBooksByPriceGreaterThan(30F);
        Book b = bookService2.getMaxIdBook();

        log.info("bs1:{},bs2:{},bs3:{},bs4:{}",bs1,bs2,bs3,bs4);
        log.info("b:{}",b);
    }
    @GetMapping("/save")
    public void save() {
        Book book = new Book();
        book.setAuthor("鲁迅");
        book.setName("呐喊");
        book.setPrice(23F);
        bookService2.addBook(book);
    }

   /* @GetMapping("/bookOps")
    public void bookOps() {
        Book b1 = new Book();
        b1.setName("西厢记");
        b1.setAuthor("王实甫");
        int i = bookService.addBook(b1);
        System.out.println("addBook>>>" + i);

        Book b2 = new Book();
        b2.setId(1);
        b2.setName("朝花夕拾");
        b2.setAuthor("鲁迅");
        int updateBook = bookService.updateBook(b2);
        System.out.println("updateBook>>>"+updateBook);

        Book b3 = new Book();
        b3.setId(1);
        b3.setName("红楼梦");
        b3.setAuthor("曹雪芹");
        int updateBook3 = bookService.updateBook(b3);
        System.out.println("updateBook3>>>"+updateBook3);

        Book b4 = bookService.getBookById(1);
        System.out.println("getBookById>>>"+b4);

        int delete = bookService.deleteBookById(2);
        System.out.println("deleteBookById>>>"+delete);

        List<Book> allBooks = bookService.getAllBooks();
        System.out.println("getAllBooks>>>"+allBooks);
    }*/
}
